#!/bin/bash
Dir="/home/radius/radius_iGuangdong/var/log/radius"

#定义radius认证日志
WFT_error_log(){
Date=`date -d "1 day ago" +"%Y-%m-%d"`
log_file="${Dir}/windfind.${Date}.log"
RadiuErr_count=`awk '/ERROR/{print $0}' ${log_file} | wc -l`

#初始化文件
echo " " > /home/rduser/WFT_error_log
echo " " > /home/rduser/Radius_error_log



if ((${RadiuErr_count}==0)); then
	RadiuErr="null"
else
	# RadiuErr=`awk '/ERROR/{print $0}' ${log_file}`
	# printf "%s\n\n\n" "${RadiuErr}" >  /home/rduser/WFT_error_log
	awk '/ERROR/{print $0}' ${log_file} >  /home/rduser/WFT_error_log

#判断错误用来自场点

	ERROR_USER=(`awk -F[" ".] '{print $12}' /home/rduser/WFT_error_log | awk '!a[$0]++'`)
	#echo ${ERROR_USER[@]} >> /home/rduser/WFT_error_log
	FILE="*-`date -d "1 day ago" +"%Y%m%d"`"
	printf "\n\n\n">> /home/rduser/WFT_error_log
	local -a Jxw
	local -a Sfdy
	local -a Zjgc
	local -a Dghx
	local -a Yac

	for i in ${ERROR_USER[@]}; 
	do
		#Error_log=`find /home/radius/radius_iGuangdong/var/log/radius/radacct -name ${FILE} | xargs grep "${i}" | awk -F [/] '{print $2}' |uniq`
		Error_log=`cd /home/radius/radius_iGuangdong/var/log/radius/radacct && find . -name ${FILE}  | xargs grep "${i}" |awk -F [/] '{print $2}' |uniq`
		case "${Error_log}" in
			"10.65.0.253" )
				#printf "%s  来自10.65.0.253(经信委)\n" "${i}">> /home/rduser/WFT_error_log
				Jxw+=(${i})
				;;
			"110.65.1.9" )
				#printf "%s  来自110.65.1.9(省府大院)\n" "${i}">> /home/rduser/WFT_error_log
				Sfdy+=(${i})
				;;
			"121.8.183.252" )
				#printf "%s  来自121.8.183.252(正佳广场)\n" "${i}">> /home/rduser/WFT_error_log
				Zjgc+=(${i})				
				;;
			"121.10.1.178" )
				#printf "%s  来自121.10.1.178(东莞环迅)\n" "${i}">> /home/rduser/WFT_error_log
				Dghx+=(${i})				
				;;
			"10.64.1.241|10.64.1.242" )
				#printf "%s  来自10.64.1.241|10.64.1.242(云AC)\n" "${i}">> /home/rduser/WFT_error_log
				Yac+=(${i})				
				;;				
		esac
	done
	printf "来自10.65.0.253(经信委)   帐号错误数:%s 个\n %s\n" "${#Jxw[@]}" "${Jxw[*]}">> /home/rduser/WFT_error_log
	printf "来自110.65.1.9(省府大院)   帐号错误数:%s 个\n %s\n" "${#Sfdy[@]}" "${Sfdy[*]}">> /home/rduser/WFT_error_log
	printf "来自121.8.183.252(正佳广场)   帐号错误数:%s 个\n %s\n" "${#Zjgc[@]}" "${Zjgc[*]}">> /home/rduser/WFT_error_log
	printf "来自121.10.1.178(东莞环迅)   帐号错误数:%s 个\n %s\n" "${#Dghx[@]}" "${Dghx[*]}">> /home/rduser/WFT_error_log
	printf "来自10.64.1.241|10.64.1.242(云AC)   帐号错误数:%s 个\n %s\n" "${#Yac[@]}" "${Yac[*]}">> /home/rduser/WFT_error_log
fi
}

Radius_error_log(){
#定义radius系统日志
RadiusLogDate=`date -d "1 day ago" | awk '{print $1" "$2" "$3}'`
Radius_log_file="${Dir}/radius.log"
RadiuLogErr_count=`awk "/${RadiusLogDate}.* Ignoring request to/" ${Radius_log_file} | wc -l`
if ((${RadiuLogErr_count}==0)); then
	RadiuLogErr="null"
else
	RadiuLogErr=`awk "/${RadiusLogDate}.* Ignoring request to/"'{(++S[$22])} END {for (key in S) print key"\t",S[key]"次"}' ${Radius_log_file}`
	printf "%s\n" "${RadiuLogErr}" > /home/rduser/Radius_error_log
fi
}

Send_Mail(){
if ((${RadiuErr_count}>0)) || ((${RadiuLogErr_count}>0)) ; then
	cd /home/rduser/alertscripts
	./pyemail.py "jc.wu@windfindtech.com" "`echo ${HOSTNAME}` check error" "`printf "Radius认证ERROR日志\n %s \n\n\n radius.log不合法nasip请求日志\n %s" "${RadiuErr}" "${RadiuLogErr}"`"
fi
}

WFT_error_log
Radius_error_log

